package com.zhoug.demo.viewpermission.page;

import android.os.Bundle;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.zhoug.common.utils.JsonUtils;
import com.zhoug.demo.viewpermission.R;
import com.zhoug.viewpermission.BindMenu;
import com.zhoug.viewpermission.Callback;
import com.zhoug.viewpermission.MenuOwnerHelper;
import com.zhoug.viewpermission.NavigationMenu;
import com.zhoug.viewpermission.ViewPermission;

import java.util.List;

/**
 * @author: zhoug
 * @since: 2025-06-13
 */
@Route(path = "/yplt/bookTab")
@BindMenu(name = "测试权限页面", code = "test", path = "/yplt/bookTab", permission = "userManager")
public class TestActivity extends MyBaseActivity {
    private MenuOwnerHelper mMenuOwnerHelper;
    private TextView mTvTitle;
    private TextView mTextView;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_test;
    }

    @Override
    protected void findViews() {
        mTvTitle = findViewById(R.id.tv_title);
        mTextView = findViewById(R.id.textView);
    }

    @Override
    protected void onCreate1(Bundle savedInstanceState) {
        mMenuOwnerHelper = new MenuOwnerHelper(this);
        mMenuOwnerHelper.setMenuCallback(new Callback<NavigationMenu>() {
            @Override
            public void onSuccess(NavigationMenu data) {
                if (data != null) {
                    mTvTitle.setText(data.getName());
                } else {
                    toast("没有查询到菜单");
                }
            }

            @Override
            public void onFailure(String error) {
                toast("获取菜单失败:" + error);
            }
        });

        mMenuOwnerHelper.addOnPermissionChangeListener(() -> {
            List<ViewPermission> permissions = mMenuOwnerHelper.getPermissions();
            mTextView.setText(JsonUtils.toJson(permissions));

        });

        mMenuOwnerHelper.loadMenu();
        mMenuOwnerHelper.loadPermission();

    }


}
